package common;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class SocketWrapper
{
	public Socket socket = null;	
	public BufferedReader in = null;
	public PrintWriter out = null;		
	
	
	
	public void close() throws Exception
	{
		try
		{
			socket.close();
		} // TRY
		catch(Exception e)
		{
			throw e;
		} // CATCH		
		
	}
	
	public String readLine() throws Exception
	{
		try
		{
			String s = in.readLine();
			return s;
		} // TRY
		catch(Exception e)
		{
			throw e;			
		} // CATCH		
		
	}

	public void println(String s) throws Exception
	{
		try
		{
			out.println(s);
		} // TRY
		catch(Exception e)
		{
			throw e;			
		} // CATCH		
		
	}

	public static SocketWrapper dup(Socket socket) throws Exception
	{
		SocketWrapper result = new SocketWrapper();
		
		try
		{
			result.socket = socket;
			result.in = new BufferedReader(new InputStreamReader(result.socket.getInputStream()));        
			result.out = new PrintWriter(result.socket.getOutputStream(), true);
			
			return result;
		} // TRY
		catch(Exception e)
		{
			throw e;
		} // CATCH		
		
	}
	
	public static SocketWrapper connect(String URL, int port) throws Exception
	{
		SocketWrapper result = new SocketWrapper();
		
		try
		{
			result.socket = new Socket(URL, port);			
			result.in = new BufferedReader(new InputStreamReader(result.socket.getInputStream()));        
			result.out = new PrintWriter(result.socket.getOutputStream(), true);
			
			return result;
		} // TRY
		catch(Exception e)
		{
			throw e;
		} // CATCH		
	}
}
